package com.ai.service;

import com.ai.exception.GraceException;
import com.ai.grace.result.ResponseStatusEnum;
import com.ai.mapper.InterviewerMapper;
import com.ai.bo.InterViewerBo;
import com.ai.mapper.JobMapper;
import com.ai.mapper.QuestionLibMapper;
import com.ai.service.impl.InterviewerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wst.ai.pojo.interviewer.InterViewer;
import wst.ai.pojo.job.Job;
import wst.ai.pojo.question.QuestionLib;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class InterviewerService implements InterviewerServiceImpl {
    @Autowired
    private InterviewerMapper interviewerMapper;
    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private QuestionLibMapper questionLibMapper;
    @Override
    public void createOrUpdate(InterViewerBo interViewerBo) {
        InterViewer interViewer = new InterViewer();
        BeanUtils.copyProperties(interViewerBo,interViewer);
        interViewer.setUpdatedTime(LocalDateTime.now());
        if (StringUtils.isBlank(interViewer.getId())){
            interViewer.setCreateTime(LocalDateTime.now());
            interviewerMapper.insert(interViewer);
        }else {
            interviewerMapper.updateById(interViewer);
        }
    }

    @Override
    public List<InterViewer> getAllInterviewer() {
        QueryWrapper<InterViewer> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("updated_time");
        List<InterViewer> interViewerList = interviewerMapper.selectList(wrapper);
        return interViewerList;
    }

    /**
     * 删除面试官
     * @param id
     */
    @Override
    public void delete(String id) {
//        先判断有没有题库或职位在使用此面试官
        QueryWrapper<Job> wrapper = new QueryWrapper<>();
        wrapper.eq("interviewer_id",id);
        List<Job> jobList = jobMapper.selectList(wrapper);
        QueryWrapper<QuestionLib> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("interviewer_id",id);
        List<QuestionLib> questionLibList = questionLibMapper.selectList(wrapper1);
    if (questionLibList.size()!=0||jobList.size()!=0){
        GraceException.display(ResponseStatusEnum.CAN_NOT_DELETE_INTERVIEWER);
    }
        interviewerMapper.deleteById(id);
    }
}
